/**
 * 用户相关 Mock 数据
 */
import Mock from 'mockjs'

// 模拟用户数据
const users = [
  {
    id: 1,
    username: 'admin',
    password: '123456',
    nickname: '管理员',
    avatar: 'https://avatars.githubusercontent.com/u/1?v=4',
    email: 'admin@vilan.com',
    phone: '13800138000',
    roles: ['admin'],
    permissions: ['*:*:*']  // 超级管理员，拥有所有权限
  },
  {
    id: 2,
    username: 'editor',
    password: '123456',
    nickname: '编辑员',
    avatar: 'https://avatars.githubusercontent.com/u/2?v=4',
    email: 'editor@vilan.com',
    phone: '13800138001',
    roles: ['editor'],
    permissions: ['system:user:view', 'system:role:view']
  }
]

export default [
  // 用户登录
  {
    url: '/api/auth/login',
    method: 'post',
    response: ({ body }) => {
      const { username, password } = body
      
      const user = users.find(
        u => u.username === username && u.password === password
      )
      
      if (!user) {
        return {
          code: 401,
          message: '用户名或密码错误',
          data: null
        }
      }
      
      // 生成 token（实际应该由后端生成）
      const token = Mock.Random.guid()
      
      return {
        code: 200,
        message: '登录成功',
        data: {
          token,
          user: {
            id: user.id,
            username: user.username,
            nickname: user.nickname,
            avatar: user.avatar,
            email: user.email,
            phone: user.phone,
            roles: user.roles
          }
        }
      }
    }
  },
  
  // 获取用户信息
  {
    url: '/api/auth/user-info',
    method: 'get',
    response: () => {
      // 这里简化处理，实际应该根据 token 获取用户信息
      const user = users[0]
      
      return {
        code: 200,
        message: '获取成功',
        data: {
          id: user.id,
          username: user.username,
          nickname: user.nickname,
          avatar: user.avatar,
          email: user.email,
          phone: user.phone,
          roles: user.roles,
          permissions: user.permissions
        }
      }
    }
  },
  
  // 用户登出
  {
    url: '/api/auth/logout',
    method: 'post',
    response: () => {
      return {
        code: 200,
        message: '登出成功',
        data: null
      }
    }
  }
]

